package com.company.psm.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.company.psm.entity.Role;
import com.company.psm.entity.Store;
import com.company.psm.entity.User;
import com.company.psm.genericdao.CustomGenericDAO;
import com.company.psm.service.UserManagement;
import com.company.psm.util.Util;

@Service(value="userManagement")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class UserManagementImpl extends CustomGenericDAO<Object> implements UserManagement{

	public Object find(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object[] find(Long... ids) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getReference(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object[] getReferences(Long... ids) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean removeById(Long id) {
		// TODO Auto-generated method stub
		return false;
	}

	public void removeByIds(Long... ids) {
		// TODO Auto-generated method stub
		
	}
	@Transactional(readOnly = true)
	public List<User> getAllUser() {
		// TODO Auto-generated method stub
		return _all(User.class);
	}
	@Transactional(readOnly = false)
	public void addUser(String fullname,String userName, String password, String roleId, String email, String storeId) {
			if(userName!=null){
				User user = new User();
				user.setAccount(userName);
				user.setFullname(fullname);
				user.setEmail(email);
				user.setPassword(password);
				Role role = _load(Role.class, Long.parseLong(roleId));
				user.setRole(role);
				Store store = _load(Store.class, Long.parseLong(storeId));
				user.setStore(store);
				_save(user);
			}
		
	}
	@Transactional(readOnly = false)
	public void deleteUser(long userId) {
		System.err.println("deleteUser with id=" + userId);
		_deleteById(User.class, userId);
	}
	@Transactional(readOnly = false)
	public void updateUser(String userID,String fullname, String userName,String pass, String roleId, String email) {
		try{
			User user = _get(User.class, Long.parseLong(userID));
			user.setAccount(userName);
			user.setFullname(fullname);
			user.setEmail(email);
			System.err.println("Encript = " + pass.equals(Util.encryptMD5("")));
			if(pass!=null && !pass.equals(Util.encryptMD5(""))){
				user.setPassword(pass);
			}
			user.setRole(_load(Role.class, Long.parseLong(roleId)));
			_update(user);
		}catch(Exception ex){
		}
	}

	@Transactional(readOnly = true)
	public User getUserById(long userId) {
		// TODO Auto-generated method stub
		return _get(User.class, userId);
	}
	@Transactional(readOnly = false)
	public List<User> search(String searchKey) {
		List<User> listSearchUser = new ArrayList<User>();
		List<User> listUser = getAllUser();
		if(searchKey!=null){
			for (User user : listUser) {
				if(user.getAccount().contains(searchKey)){
					listSearchUser.add(user);
				}
			}
		}else{
			listSearchUser = listUser;
		}
		return listSearchUser;
	}
	@Transactional(readOnly = true)
	public User getUserByAccount(String account) {
		List<User> listUser = getAllUser();
		User getUser = new User();
		if(account!=null){
			for (User user : listUser) {
				if(user.getAccount().equals(account)){
					getUser = user;
					break;
				}
			}
		}
		return getUser;
	}
	public List<User> getAllStoreUser(String storeId) {
		List<User> users = new ArrayList<User>();
		for (User user : getAllUser()) {
			if(String.valueOf(user.getStore().getId()).equals(storeId)){
				users.add(user);
			}
		}
		return users;
	}

	public List<User> searchRole(String searchKey, String storeId) {
		List<User> listSearchUser = new ArrayList<User>();
		List<User> listUser = new ArrayList<User>(); 
		if(storeId.equals("0")){
			listUser = getAllUser();
		}else{
			listUser = getAllStoreUser(storeId);
		}
		if(searchKey!=null){
			for (User user : listUser) {
				if(user.getRole().getId() == Long.parseLong(searchKey)){
					listSearchUser.add(user);
				}
			}
		}else{
			listSearchUser = listUser;
		}
		return listSearchUser;
	}

	public List<User> getAllStoreUserByRole(long storeId, String role) {
		List<User> users = new ArrayList<User>();
		for (User user : getAllUser()) {
			if(user.getStore().getId()== storeId && user.getRole().getCode().equals(role)){
				users.add(user);
			}
		}
		System.err.println("Size = " + users.size());
		return users;
	}
}
